//
//  ViewController.m
//  UITableViewHomework
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "PersonViewController.h"

#define kTableView @"resue"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic , retain)NSMutableArray *arrOfname;
@property(nonatomic,strong)NSMutableArray *groupNameArray;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
      [super viewDidLoad];
      self.navigationItem.rightBarButtonItem = self.editButtonItem;
      self.navigationItem.title = @"通讯录";
      _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
      [self.view addSubview:_tableView];
      _tableView.delegate = self;
      _tableView.dataSource = self;
      [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableView];
      NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
      _arrOfname = [NSMutableArray arrayWithContentsOfFile:path];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
      [super setEditing:editing animated:animated];
      [_tableView setEditing:editing animated:animated];
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
          [_arrOfname removeObjectAtIndex:indexPath.row];
          [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    }];
      return @[rowAction];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

      return _arrOfname.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView forIndexPath:indexPath];
      NSDictionary *dic = [self.arrOfname objectAtIndex:indexPath.row];
      cell.textLabel.text = dic[@"name"];
          return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      PersonViewController *person = [[PersonViewController alloc]init];
      [self.navigationController pushViewController:person animated:YES];
   
}
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
      //数组内容要和分区个数一样 才有效果
      return @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"X", @"Y", @"Z"];
}



//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//
//      return [_arrOfname objectAtIndex:section];
//}
@end
